package com.zk.controller;
import com.zk.model.Admin;
import com.zk.model.Goods;
import com.zk.model.Order;
import com.zk.model.Pj;
import com.zk.service.AdminService;
import com.zk.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class AdminasController {
    @Resource
    private UserService userService;
    @Resource
    private AdminService adminService;
//    管理员登录
    @PostMapping("/hlongin")
    public String adminLongin(String adminName, String adminPsd, HttpSession session){
        Admin admin=adminService.selectAdmin(adminName,adminPsd);
        session.setAttribute("admin",admin);
        if (admin==null){
            return "hlongin";
        }
        return "hindex";
    }
    //    主页
    @GetMapping("/hlongin")
    public String hongin(){
        return "hlongin";
    }
//    主页
    @GetMapping("/hindex")
    public String hindex(){
        return "hindex";
    }
//    添加商品页面
    @GetMapping("/haddImg")
    public String haddImg(){
        return "haddImg";
    }
    // 添加商品
    @GetMapping("/addGoods")
    public String addGoods(String name,String price,String img,String stock,String size){
        int num=adminService.addGood(name,Integer.valueOf(price),img,Integer.valueOf(stock),size);
        return "haddImg";
    }
    //    订单管理
    @GetMapping("/horder")
    public String horder(Model model){
        List<Order> orderList=adminService.selectOrders();
        model.addAttribute("orderList",orderList);
        return "horder";
    }
    //   评价管理
    @GetMapping("/hsmsInfo")
    public String hsmsInfo(Model model){
        List<Pj> pjList=userService.selectPj();
        model.addAttribute("pjList",pjList);
        return "hsmsInfo";
    }
    // 商品管理
    @GetMapping("/huserInfo")
    public String huserInfo(Model model){
        List<Goods> goodsList=adminService.selectGoods();
        model.addAttribute("goodsList",goodsList);
        return "huserInfo";
    }
    //跳转填写订单号
    @GetMapping("/htrack")
    public String goHtrack(HttpSession session,String id){
        session.setAttribute("orderId",id);
        return "htrack";
    }
    //添加快递单号
    @GetMapping("/addState")
    public String addState(HttpSession session,String trackNub,String trackName,Model model){
        String orderId=session.getAttribute("orderId").toString();
        String orderState="已发货";
       int num=adminService.addState(orderState,trackNub,trackName,Integer.valueOf(orderId));
        List<Order> orderList=adminService.selectOrders();
        model.addAttribute("orderList",orderList);
        return num==1?"horder":"";
    }
}
